package db

import (
	"manage-server/global"
	"manage-server/model/manage_model"
	"manage-server/util"
)

// 初始化后台管理默认数据
func InitManage() {
	db := GetManageDB()
	// 创建默认角色
	manage_user_role := manage_model.ManageUserRole{
		Name:        "超级管理员",
		Description: "超级管理员(禁止删除)",
	}
	tmp_user_role := manage_model.ManageUserRole{}
	db.Model(&manage_model.ManageUserRole{}).Where("id = 1").First(&tmp_user_role)
	if tmp_user_role.ID == 1 {
		return
	}
	db.Create(&manage_user_role)
	// 创建默认用户
	passowrd, err := util.GenerateFromPassword(global.App.Config.Default.Passowrd)
	if err != nil {
		panic("创建默认用户加密失败")
	}
	manage_user := manage_model.ManageUser{
		Username: global.App.Config.Default.UserName,
		Password: passowrd,
		Nickname: "超级管理员",
		Fullname: "超级管理员",
		Status:   1,
		IsSuper:  1,
		RoleId:   1,
	}
	tmp_user := manage_model.ManageUser{}
	db.Model(&manage_model.ManageUser{}).Where("username = admin").First(&tmp_user)
	if tmp_user.Username == "admin" {
		return
	}
	db.Create(&manage_user)
}
